package com.yangcangclub.trade.domain;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.trade.base.TradeBaseDomain;
import com.yangcangclub.trade.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/2/27.
 */
@Component
public class TradeRefundDomain extends TradeBaseDomain {

    @Autowired
    private TradeAddressDomain tradeAddressDomain;
    @Autowired
    private TradeDetailsDomain tradeDetailsDomain;
    @Autowired
    private TradeReqLogsDomain tradeReqLogsDomain;

    /**
     * @param tradeRefund
     * @return
     */
    public int insertTradeRefund(TradeRefund tradeRefund){
        super.getMasterJdbc().insert("trade.TradeRefundMapper.insertTradeRefund",tradeRefund);
        int id = tradeRefund.getId();
        if(id == 0){
            throw new ApolloException(MessageEnum.ORDER_ADD_FAILURE);
        }
        return id;
    }

    public int updateTradeRefund(TradeRefund tradeRefund){
        super.getMasterJdbc().update("trade.TradeRefundMapper.updateTradeRefund",tradeRefund);
        int id = tradeRefund.getId();
        if(id == 0){
            throw new ApolloException(MessageEnum.ORDER_ADD_FAILURE);
        }
        return id;
    }

    /**
     * @param id
     * @param selectType
     * @return
     */
    public TradeRefund selectTradeRefundById(int id,String selectType){
        Map m = Maps.newHashMap();
        if(!"manager".equals(selectType) && !"api".equals(selectType)){m.put("selectType","manager");}else{m.put("selectType",selectType);}
        m.put("id",id);
        return super.getSlaveJdbc().selectOne("trade.TradeRefundMapper.selectTradeRefundById",m);
    }

    /**
     * @param tradeNo
     * @param selectType
     * @return
     */
    public List<TradeRefund> selectTradeRefundByTradeNo(String tradeNo,String selectType){
        Map m = Maps.newHashMap();
        if(!"manager".equals(selectType) && !"api".equals(selectType)){m.put("selectType","manager");}else{m.put("selectType",selectType);}
        m.put("tradeNo",tradeNo);
        return super.getSlaveJdbc().selectList("trade.TradeRefundMapper.selectTradeRefundByTradeNo",m);
    }

    /**
     * @param refundNo
     * @param selectType
     * @return
     */
    public TradeRefund selectTradeRefundByRefundNo(String refundNo,String selectType){
        Map m = Maps.newHashMap();
        if(!"manager".equals(selectType) && !"api".equals(selectType)){m.put("selectType","manager");}else{m.put("selectType",selectType);}
        m.put("refundNo",refundNo);
        return super.getSlaveJdbc().selectOne("trade.TradeRefundMapper.selectTradeRefundById",m);
    }

    /**
     * @param m
     * @param start
     * @param pageSize
     * @param selectType
     * @return
     * */
    public List<TradeRefundVo> selectPageBreakByCondition(Map m, int start, int pageSize,String selectType){
        if(start<0||pageSize>50||pageSize<0){
            throw new ApolloException(MessageEnum.PARMA_RANGE_ERROR);
        }
        m.put("start",start);
        m.put("pageSize", pageSize);
        if(!"manager".equals(selectType) && !"api".equals(selectType)){m.put("selectType","manager");}else{m.put("selectType",selectType);}
        List<TradeRefund> tradeRefundList = super.getSlaveJdbc().selectList("trade.TradeRefundMapper.selectPageBreakByCondition",m);
        List<TradeRefundVo> list =  new ArrayList<>();
        for(TradeRefund tradeRefund:tradeRefundList){
            TradeRefundVo tradeRefundVo = new TradeRefundVo();
            tradeRefundVo.setTradeRefund(tradeRefund);
            List<TradeDetails> tradeDetailsList = tradeDetailsDomain.selectTradeDetailsByTradeNo(tradeRefund.getTradeNo());
            tradeRefundVo.setTradeDetailsList(tradeDetailsList);
            list.add(tradeRefundVo);
        }
        return list;
    }

    /**
     * @param map
     * @return
     */
    public int selectPageBreakByConditionCount(Map map){
        int total = super.getSlaveJdbc().selectOne("trade.TradeRefundMapper.selectPageBreakByConditionCount",map);
        return total;
    }
}
